package com.yyq.adms.bzgf.action;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.yyq.adms.bzgf.bpo.StandardFormationBpo;
import com.yyq.adms.bzgf.bpo.StandardPublishBpo;
import com.yyq.adms.bzgf.pojo.StandardFormation;
import com.yyq.adms.bzgf.pojo.StandardPublish;
import com.yyq.adms.gf.pojo.OwnLaw;
import com.yyq.adms.sysmanage.pojo.ExcelBean;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;

@Controller("standardFormationAction")
@Scope("prototype")
public class StandardFormationAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private StandardFormation po=new StandardFormation();
	private StandardFormationBpo bpo;
	private StandardPublishBpo stdpubbpo;	
	
	public StandardPublishBpo getStdpubbpo() {
		return stdpubbpo;
	}

	@Resource(name="standardPublishBpo")
	public void setStdpubbpo(StandardPublishBpo stdpubbpo) {
		stdpubbpo.setCls(StandardPublish.class);
		this.stdpubbpo = stdpubbpo;
	}

	@Resource(name="standardFormationBpo")
	public void setBpo(StandardFormationBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	public StandardFormation getPo() {
		return po;
	}

	public void setPo(StandardFormation po) {
		this.po = po;
	}

	@Override
	public String execute() throws Exception {
		super.setObj(po);
		super.setBpo(bpo);
		String ret = super.execute();
		po = (StandardFormation)super.getObj();
		return ret;
	}
	/*
	@Override
	public String data() {
		String result =  super.data();
		super.
		PageFormData formData= this.getFormData();
		List _templist = formData.getData();
		List _list = new ArrayList();
		for(int i = 0; i < _templist.size(); i ++) {
			StandardFormation _paper = (StandardFormation)_templist.get(i);
			Date _date = _paper.getStartDate();
			String _year = YYBUtils.getFormatDateYear(_date);
			_paper.setBelongYear(_year);
			_list.add(_paper);			
		}
		formData.setData(_list);
		
		return result;
	}*/
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from StandardFormation order by belongYear desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"'  and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"'  and rsd.sentDepId="+user.getOrgId()+")  order by obj.belongYear desc";
		}
		
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		return ActionType.ACTION_DATA_LIST;
	}
	
	public String goAdd()
	{
		String[] type = (String[])this.getFormValue("type");
		if(type != null && "publish".equals(type[0])){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			this.setFormValue("nowDate", sdf.format(new Date()));
			String sentDep = po.getSentDep();
			po = (StandardFormation)bpo.findByPk(po.getId());
			po.setSentDep(sentDep);
			this.setObj(po);
			return "publishAdd";
		}else{
			return ActionType.ACTION_GO_ADD;
		}
	}
	
	@Override
	public String update() {
		String state = this.po.getSchedules();
		Long sourceID = this.po.getId();
		String result =  super.update();
		String updateFlag = (String)this.getFormValue("updateFlag");
		if(state!=null && state.equals("7") && "TRUE".equals(updateFlag)) {
			// 正式颁布则添加到“标准规范发布”
			String tableName = stdpubbpo.getTableName(StandardPublish.class);
			List _tempList = stdpubbpo.searchSql("select * from " + tableName + " where sourceid = " + sourceID);
			
			if(_tempList.size() == 0 ) {
				this.setMsg("{processId:\"2\",msg:\"更新成功\",sourceId:\""+sourceID+"\",sentDep:\""+po.getSentDep()+"\"}");
				
				/*StandardPublish target = new StandardPublish();
				target.setStandardPublishName(po.getStandardName());
				target.setEditorOrActor(po.getEditorOrActor());
				target.setCreateOrModify(po.getCreateOrModify());
				target.setJoinDep(po.getJoinDep());
				target.setMembers(po.getMembers());
				target.setBelongPlateFormID(po.getBelongPlateFormID());
				//target.setSentDep(po.getSentDep());
				target.setSourceID(sourceID.intValue());
				target.setPublishDate(new Date());
				target.setState("1"); //初始状态为“已发布”
				//默认设置为地方级
				target.setDegree("3");
				Serializable id = stdpubbpo.save(target);	
				if(id != null){
					String sentDep = po.getSentDep();
					List sds = new ArrayList();
					processSentDep(sds, tableName, sentDep, id);
					if(sds.size()>0){
						Serializable[] ids = bpo.save(sds.toArray());
					}
				}*/
			}
		}
		return result;
	}

	public String exportAll(){
		String hql = getExportHql("belongYear");
		List list = bpo.search(hql);
		String temp = "";
		ExcelBean eb = null;
		List datas = new ArrayList();
		for(int i=0;i<list.size();i++){
			StandardFormation obj = (StandardFormation)list.get(i);
			processObjSchedules(obj);
			if(!temp.equals(obj.getBelongYear())){
				temp = obj.getBelongYear();
				if(eb != null){
					datas.add(eb);
				}
				eb = new ExcelBean(obj.getBelongYear());
			}
			eb.getData().add(obj);
			if(i == list.size()-1){
				datas.add(eb);
			}
		}
		Map beans = new HashMap();
		beans.put("datas", datas);
		beans.put("isGlobal", isSentDepRight(null));
		exportXls(beans,"标准规范编制批量导出数据",null);
		return null;
	}

	private void processObjSchedules(StandardFormation obj) {
		if("7".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
			obj.setStep3("√");
			obj.setStep4("√");
			obj.setStep5("√");
			obj.setStep6("√");
			obj.setStep7("√");
		}else if("6".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
			obj.setStep3("√");
			obj.setStep4("√");
			obj.setStep5("√");
			obj.setStep6("√");
		}else if("5".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
			obj.setStep3("√");
			obj.setStep4("√");
			obj.setStep5("√");
		}else if("4".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
			obj.setStep3("√");
			obj.setStep4("√");
		}else if("3".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
			obj.setStep3("√");
		}else if("2".equals(obj.getSchedules())){
			obj.setStep1("√");
			obj.setStep2("√");
		}else if("1".equals(obj.getSchedules())){
			obj.setStep1("√");
		}
		
		if("1".equals(obj.getEditorOrActor())){
			obj.setEditorOrActor("主编");
		}else if("2".equals(obj.getEditorOrActor())){
			obj.setEditorOrActor("参编");
		}
		
		if("1".equals(obj.getCreateOrModify())){
			obj.setCreateOrModify("制定");
		}else if("2".equals(obj.getCreateOrModify())){
			obj.setCreateOrModify("修订");
		}
	}
}
